package com.jetbrains.kmpapp.platform

import platform.UIKit.UIApplication
import platform.UIKit.UISceneActivationStateForegroundActive
import platform.UIKit.UIViewController
import platform.UIKit.UIWindow
import platform.UIKit.UIWindowScene

val UIApplication.currentKeyWindow: UIWindow?
    get() {
        // 过滤出活动的窗口场景
        val windowScene = connectedScenes.first {
            (it as? UIWindowScene)?.activationState == UISceneActivationStateForegroundActive
        } as? UIWindowScene
        // 获取场景中的主窗口
        return windowScene?.windows?.first { (it as? UIWindow)?.isKeyWindow() == true } as? UIWindow
    }

val UIApplication.currentRootViewController: UIViewController?
    get() = currentKeyWindow?.rootViewController